package com.bee.mydiary.camera;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.bee.mydiary.R;

public class CameraActivity extends Activity implements OnClickListener {
	private String TAG = "CameraActivity";
	private Button mstart;
	private Button mget;
	private Button mshow;
	private LinearLayout mLinearLayout = null;
	private CameraView mCameraView = null;

	public static final String external_path = Environment
			.getExternalStorageDirectory().toString() + "/mydiary/";
	private Camera mycamera;
	private ContentResolver mContentResolver;

	private Camera.PictureCallback picture = new Camera.PictureCallback() {

		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			savePIc(data, camera);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.v(TAG, "CameraActivity onCreate()");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		setContentView(R.layout.camera_layout);
		mstart = (Button) findViewById(R.id.start);
		mget = (Button) findViewById(R.id.get);
		mshow = (Button) findViewById(R.id.show);
		mLinearLayout = (LinearLayout) findViewById(R.id.camera);

		mstart.setOnClickListener(this);
		mget.setOnClickListener(this);
		mshow.setOnClickListener(this);	
		mContentResolver = getContentResolver();

	}
	
	@Override
	protected void onResume() {
		Log.v(TAG, "CameraActivity onResume()");
		// TODO Auto-generated method stub
		super.onResume();
		try{
			mycamera = Camera.open();	
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}		
//		mCameraView = new CameraView(this, mycamera);
	}
	
	@Override
	protected void onStop() {
		Log.v(TAG, "CameraActivity onStop()");
		// TODO Auto-generated method stub
		super.onStop();
		mLinearLayout.removeAllViews();
		mget.setEnabled(false);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

	}
	
	@Override
	protected void onDestroy() {
		Log.v(TAG, "CameraActivity onDestroy()");
		// TODO Auto-generated method stub
		super.onDestroy();	
		if(mCameraView != null){
			mCameraView = null;
		}
		if(mycamera != null){
			mycamera = null;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == mstart) {
			mLinearLayout.removeAllViews();
			mCameraView = new CameraView(this, mycamera);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.FILL_PARENT);
			mLinearLayout.addView(mCameraView, params);
			mget.setEnabled(true);
		} else if (v == mget) {
			if (mycamera != null) {
				mycamera.takePicture(null, null, picture);
			}
		} else if (v == mshow) {
			Intent i = new Intent(CameraActivity.this,ShowPicActivity.class);
			startActivity(i);
		}
	}

	public void savePIc(byte[] data, Camera camera) {
		try {
			Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0,
					data.length);
			String filename;
			String path = Environment.getExternalStorageDirectory().toString();
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHMMSS");
			filename = sdf.format(date);
			OutputStream fout = null;
			File destDir = new File(path + "/mydiary");
			  if (!destDir.exists()) {
			   destDir.mkdirs();
			  }
			File file = new File(path + "/mydiary", "/" + filename + ".jpg");
			fout = new FileOutputStream(file);
			bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 70, fout);
			fout.flush();
			fout.close();

			MediaStore.Images.Media.insertImage(mContentResolver,
					file.getAbsolutePath(), file.getName(), file.getName());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			camera.startPreview();
		}
	}
}
